<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");


	$db = NEW myDBs();
	$db->selectDB("account");

                            
                $sql = "SELECT cheque_no,  `bank_name` ,  `branch_name`, cheque_status, paid_status , SUM( aia ) as aia, SUM( tax ) as tax, 
                                    sum(other2) as other2, sum(cremation2) as cremation2, sum(ghb) as ghb, 
                                    sum(bongkum) as bongkum, sum(loan) as loan, sum(cooperative) as cooperative, 
                                    sum(welfare) as welfare, sum(cremation1) as cremation1
                        FROM tbl_debt
                        GROUP BY cheque_no,  `bank_name` ,  `branch_name` , cheque_status, paid_status
                    ";
	//echo $sql;
	$result = $db->runSQL($sql);
	//$rows = $db1->fetch_object($result);

?>

<script type="text/javascript">
	$(document).ready(function(){
                $("#example").dataTable();
                }); 
	function del(cheque_no)  {
			//alert("adfs");
		if (confirm("คุณแน่ใจที่ต้องการลบรายการนี้ใช่ไหม?"))
		{
			//$.post("process_petition_salary.php", {pet_id: pet_id, task: 'del'}, function() {window.location.href="index.php?pages=frm_petition_salary&pageid=1";});
		}

	}
        
                function info(cheque_no) {
                    $('<div>').dialog2({
                        autoOpen: true,
                        title: "รายละเอียดเลขที่เช็ค " + cheque_no,
                        content: "dlg_cheque_debt_info.php?cheue_no=" + cheque_no,
                        id: "modal-printCheque"
                     });
                }

            function paidCheque(cheque_no){
		var task = "paid";
			$.post('process_status_salary.php',{task: task, cheque_no: cheque_no}, function(data) {
					window.location.href="index.php?pages=paid_salary";
			});
	}

	function approvedCheque(cheque_no){
		var task = "approvedCheque";
		
		$('<div>').dialog2({
			autoOpen: true,
			title: "อนุมัติ",
			content: "dlg_date_approved_debt.php?cheque_no=" + cheque_no + "&task=" + task,
			id: "modal-dlgDate"
		});
	}

	function disapprovalCheque(cheque_no){
		var task = "disapprovalCheque";

		$.post('process_status_salary.php',{task: task, cheque_no: cheque_no}, function(data) {
			window.location.href="index.php?pages=paid_salary";
		});

	}
	
</script>


<a href="index.php?pages=cheque_debt" class="btn" id="add_new">จัดการเช็ค</a>
<table id="example" class="table table-bordered" width="100%" >
	<caption><br></caption>
	<thead>
		<tr>
			<th>#</th>
			<th>เลขที่เช็ค</th>
                                            <th>รายการ</th>
			<th>ธนาคาร</th>
			<th>สาขา</th>
			<th>จำนวนเงิน</th>
                                               <th>ข้อมูลเพิ่มเติม</th>
			<th>action</th>
                        <th>อนุมัติเช็ค</th>
				<th>จ่ายเช็ค</th>
		</tr>
	</thead>
	<tbody>	
<?php


	$i=0;
	while ($rows = $db->fetch_object($result)) {
		$i++;
		if ($rows->tax > 0) {
                                $item = "ภาษี";
                                $amount = $rows->tax;
                            } elseif ($rows->aia > 0) {
                                $item = "AIA";
                                $amount = $rows->aia;
                            } elseif ($rows->cremation2 > 0) {
                                $item = "ก.ฌ. เทศบาล";
                                $amount = $rows->cremation2;
                            } elseif ($rows->cremation1 > 0) {
                                $item = "ก.ฌ.";
                                $amount = $rows->cremation1;
                            } elseif ($rows->welfare > 0) {
                                $item = "สวัสดิการ";
                                $amount = $rows->welfare;
                            } elseif ($rows->cooperative > 0) {
                                $item = "สหกรณ์";
                                $amount = $rows->cooperative;
                            } elseif ($rows->loan > 0) {
                                $item = "กู้เทศบาล";
                                $amount = $rows->loan;
                            } elseif ($rows->bongkum > 0) {
                                $item = "ธอ/บงคำ";
                                $amount = $rows->bongkum;
                            } elseif ($rows->ghb > 0) {
                                $item = "ธอส";
                                $amount = $rows->ghb;
                            } elseif ($rows->other2 > 0) {
                                $item = "อื่นๆ";
                                $amount = $rows->other2;
                            } else {
                                $item = "ผิดพลาด";
                                $amount = -1;
                            }
                             
        	echo "<tr>
					<td>$i</td>
					<td>$rows->cheque_no</td>
					<td>$item</td>
					<td>$rows->bank_name</td>
					<td>$rows->branch_name</td>
					<td>$amount</td>
                                                                        <td><a href='javascript:info(\"$rows->cheque_no\")'> รายละเอียด</a></td>
					<td><a href='javascript:del(\"$rows->cheque_no\")'><i class='icon-remove'></i></a>&nbsp;|&nbsp;<a href='prt_cheque_debt.php?cheque_no=$rows->cheque_no' target='_blank'><i class='icon-print'></i></a></td>
                                            ";
                                                        echo " <td>";
					
						if($rows->cheque_status == "รออนุมัติ") {
							echo "<a href='javascript:approvedCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='อนุมัติเช็ค'><i class='icon-ok'></i></a>&nbsp;<a href='javascript:disapprovalCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='ยกเลิกเช็ค'><i class='icon-remove'></i></a>
							";
							
						} else {
							echo $rows->cheque_status;
						 } 
					
				echo "</td>";
	 			echo " <td>";
					 
						if ($rows->cheque_status == "อนุมัติ") {
							if($rows->paid_status == "รอจ่าย") {
								echo "<a href='javascript:paidCheque(\"$rows->cheque_no\")' data-toggle='tooltip' title='จ่ายเช็ค'><i class='icon-ok'></i></a>";
								
							} else {
								echo $rows->paid_status;
							}
						} else {
							echo "รอสถานะเช็ค";
						}
				echo "
				</td>
				</tr>
				";
		
	}

?>

</tbody>
</table>
